<template>
  <el-dialog
    custom-class="add-car"
    v-el-drag-dialog="{ drag: true, full: false, resize: false }"
    :close-on-click-modal="false"
    :visible="true"
    title="报停启动时间"
    @close="closeMd"
    :width="'400px'">
    <el-form
      :model="fm.ps"
      :ref="fm.fname"
      :label-width="'100px'"
      @keyup.enter.native="onSubmit">
      <el-form-item
        label="启动时间"
        prop="startTime"
        :rules="[
          { required: true, message: '请选择启动时间', trigger: 'change' },
        ]">
        <el-date-picker
          v-model="fm.ps.startTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm"
          format="yyyy-MM-dd HH:mm"
          :picker-options="pickerOptions"
          placeholder="启动时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          @click="onSubmit"
          :loading="fm.sb.load"
          type="primary">
          {{ fm.sb.text }}
        </el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import elDragDialog from "@/directive/el-drag-dialog";

import * as logisticsSer from "@/api/services/logistics.service";
export default {
  name: "AddStopTime",
  directives: { elDragDialog },
  props: {myData: Object},
  data() {
    let that = this;
    return {
      // 启动时间-配置
      pickerOptions: {
        disabledDate(time) {
          const dd = that.U.str2date(that.myData.obj.destoryTime);
          dd.setDate(dd.getDate());
          return time < dd;
        },
      },

      fm: {
        fname: "addStopOffLineFm",
        sb: { load: false, text: this.myData.obj.startTime ? '修改' : '添加'},
        ps: {
          // 车辆id
          vehicleId: this.myData.obj.id, 
          // 预约开始时间
          startTime: this.myData.obj.startTime ? this.U.datefmt(this.myData.obj.startTime) : '', 
        },
      },

    };
  },
  created(){
    console.log(this.myData);
  },
  methods: {
    /* 关闭弹框 */
    closeMd() {
      this.$emit("myClick", {state: false});
    },
    // 表单提交
    onSubmit() {
      const that = this, U = that.U, fm = that.fm, sb = fm.sb, ps = U.scopy(fm.ps);

      that.$refs[fm.fname].validate((valid) => {
        if (!valid) {return false;} else {
          sb.load = true;

          logisticsSer.destoryStart(ps).then((res) => {
            sb.load = false;

            if(res.code != 1){
              U.qerror(res.msg);
            }else{
              U.qsuccess(res.msg);

              that.closeMd();
              // 调用父组件方法-重新加载数据
              that.$emit("reloadDg");
            }

            setTimeout(() => { sb.load = false; }, 10000);
          });
        }
      });
    },

  },
};
</script>